# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=mako
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.0.7
pkgrel=2
pkgdesc="A super-fast templating language that borrows the best ideas from the existing templating languages (mingw-w64)"
arch=('any')
license=('custom')
url="https://pypi.python.org/pypi/Mako"
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-markupsafe"
             "${MINGW_PACKAGE_PREFIX}-python3-markupsafe"
             "${MINGW_PACKAGE_PREFIX}-python2-beaker"
             "${MINGW_PACKAGE_PREFIX}-python3-beaker")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python2-nose"
              "${MINGW_PACKAGE_PREFIX}-python3-nose")
options=('staticlibs')
source=("https://pypi.org/packages/source/M/Mako/Mako-${pkgver}.tar.gz")
sha256sums=('4e02fde57bd4abb5ec400181e4c314f56ac3e49ba4fb8b0d50bba18cb27d25ae')

prepare() {
  cd ${srcdir}
  for pver in {2,3}; do 
    rm -rf python${pver}-build-${CARCH} | true
    cp -r "${_realname}-${pkgver}" "python${pver}-build-${CARCH}"
  done
}

build() {
  cd "${srcdir}"
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done  
}

check() {
  cd "${srcdir}"
  for pver in {2,3}; do  
    msg "Python ${pver} test for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py test
  done  
}

package_python2-mako() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2-markupsafe" "${MINGW_PACKAGE_PREFIX}-python2-beaker")
  cd "${srcdir}/python2-build-${CARCH}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1

  # fix python command in files
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_f}
  done

  mv ${pkgdir}${MINGW_PREFIX}/bin/mako-render{,2}.exe
  mv ${pkgdir}${MINGW_PREFIX}/bin/mako-render{,2}-script.py

  [[ -f ${pkgdir}${MINGW_PREFIX}/bin/mako-render.exe.manifest ]] && {
    mv ${pkgdir}${MINGW_PREFIX}/bin/mako-render{,2}.exe.manifest
    sed -e "s|mako-render|mako-render2|g" -i ${pkgdir}${MINGW_PREFIX}/bin/mako-render2.exe.manifest
  }

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/COPYING"
}

package_python3-mako() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3-markupsafe" "${MINGW_PACKAGE_PREFIX}-python3-beaker")

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1

  # fix python command in files
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_f}
  done

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/COPYING"
}

package_mingw-w64-i686-python2-mako() {
  install=${_realname}2-${CARCH}.install
  package_python2-mako
}

package_mingw-w64-i686-python3-mako() {
  install=${_realname}3-${CARCH}.install
  package_python3-mako
}

package_mingw-w64-x86_64-python2-mako() {
  install=${_realname}2-${CARCH}.install
  package_python2-mako
}

package_mingw-w64-x86_64-python3-mako() {
  install=${_realname}3-${CARCH}.install
  package_python3-mako
}
